﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using MvcMovie.Models;

namespace MvcMovie.Middleware
{
    //基于约定的中间件
    public class ConventionalMiddleware
    {
        private readonly RequestDelegate _next;

        public ConventionalMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        //TODO: 可以直接拿到数据库上下文，作数据范围验证？
        public async Task InvokeAsync(HttpContext context, MvcMovieContext db)
        {
            //业务逻辑处理
            //var keyValue = context.Request.Query["key"];

            //if (!string.IsNullOrWhiteSpace(keyValue))
            //{
            //    db.Add(new Request()
            //    {
            //        DT = DateTime.UtcNow,
            //        MiddlewareActivation = "ConventionalMiddleware",
            //        Value = keyValue
            //    });

            //    await db.SaveChangesAsync();
            //}

            await _next(context);
        }
    }
}
